package com.example.smartcity.ui.frg

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.GridLayoutManager
import com.example.smartcity.R
import com.example.smartcity.adapter.ServiceGridAdapter
import com.example.smartcity.app.App
import com.example.smartcity.http.*
import com.example.smartcity.ui.act.MainActivity
import com.example.smartcity.ui.act.NewsDetailActivity
import com.example.smartcity.ui.act.TOFHomeActivity
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_main_tab1.*


/**
 * @Author: Tailyou(祝文飞)
 * @CreateDate: 2021/10/7 14:53
 * @Description: 主页
 */
class MainEnterFrg1 : Fragment(R.layout.fragment_main_tab1) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadBannerData()
        loadServiceData()
        loadNewsTypeData()
    }

    // 读取新闻分类数据
    private fun loadNewsTypeData() {
        App.fetchData<NewsTypeResp>(
            "/prod-api/press/category/list",
            NewsTypeResp::class.java
        ) {
            renderNewsTypeData(it)
        }
    }

    // 渲染新闻分类数据
    private fun renderNewsTypeData(newsTypeResp: NewsTypeResp) {
        var newsTypes = newsTypeResp.data
        var newsTabFrgs = mutableListOf<MainNewsFrg>()
        for (item in newsTypes) {
            tabLayout.addTab(tabLayout.newTab().setText(item.name))
            newsTabFrgs.add(MainNewsFrg.newInstance(item.id))
        }
        viewPager.adapter = object : FragmentStatePagerAdapter(
            requireActivity().supportFragmentManager,
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getCount(): Int {
                return newsTabFrgs.size
            }

            override fun getItem(position: Int): Fragment {
                return newsTabFrgs[position]
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return newsTypes[position].name
            }
        }
        tabLayout.setupWithViewPager(viewPager, false)
    }

    // 读取全部服务数据
    private fun loadServiceData() {
        App.fetchData<ServiceResp>(
            "/prod-api/api/service/list",
            ServiceResp::class.java
        ) {
            renderServiceData(it)
        }
    }

    // 渲染各服务入口数据
    private fun renderServiceData(serviceResp: ServiceResp) {
        var services = serviceResp.rows.sortedByDescending { it.id }.subList(0, 9)
        var entryServices: MutableList<ServiceBean> = mutableListOf()
        entryServices.addAll(services)
        entryServices.add(ServiceBean("全部服务", "", "", 0))
        rvServiceGrid.layoutManager = GridLayoutManager(context, 5)
        rvServiceGrid.adapter =
            ServiceGridAdapter(entryServices, object : ServiceGridAdapter.OnItemClickListener {
                override fun onClick(position: Int) {
                    if (position == 9) {
                        MainActivity.switch2AllService()
                    }
                    if (position == 4) {
                        val intent = Intent(context, TOFHomeActivity::class.java)
                        startActivity(intent)
                    }
                }
            }, R.layout.layout_item_grid_service_entry)
    }

    // 读取Banner数据
    private fun loadBannerData() {
        App.fetchData<AdvBannerResp>(
            "/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=2",
            AdvBannerResp::class.java
        ) {
            renderBannerData(it)
        }
    }

    // 渲染Banner数据
    private fun renderBannerData(advBannerResp: AdvBannerResp) {
        banner.addBannerLifecycleObserver(this@MainEnterFrg1)
        banner.indicator = CircleIndicator(context)
        banner.adapter =
            object : BannerImageAdapter<AdvBannerBean>(advBannerResp.rows) {
                override fun onBindView(
                    p0: BannerImageHolder?,
                    p1: AdvBannerBean?,
                    p2: Int,
                    p3: Int
                ) {
                    App.fetchRCImage(
                        p1?.advImg,
                        p0!!.imageView,
                        error = R.mipmap.ic_more_service
                    )
                    p0!!.imageView.setOnClickListener {
                        val intent = Intent(context, NewsDetailActivity::class.java)
                        intent.putExtra("news_id", p1?.targetId)
                        startActivity(intent)
                    }
                }
            }
    }

}